<?php
//    +----------------------------------------------------------------------
//    | Copyright (c) 2015-2035 http://www.hdphp.cn All rights reserved.
//    | Licensed ( http://www.hdphp.cn/licenses/ )
//    | Author: Jack <sophia2152@qq.com>
//    | 官方网址: http://www.hdphp.cn
//    | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//    | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//    +----------------------------------------------------------------------
namespace app\controller\admin;

use app\Request;
use think\facade\Cache;
use app\services\system\admin\SystemAdminServices;

/**
 * Class：后台登陆
 * 创建人：Jack
 * 创建时间：2023/3/22 7:59 PM
 * @package app\controller\admin
 */
class Login
{
    /**
     * @var SystemAdminServices
     */
    protected $services;

    /**
     * Login constructor.
     * @param SystemAdminServices $services
     */
    public function __construct(SystemAdminServices $services)
    {
        $this->services = $services;
    }

    /**
     * 注释：getAjCaptcha
     * 创建人：Jack
     * 创建时间：2023/3/22 8:11 PM
     * @param Request $request
     * @return mixed
     */
    public function getAjCaptcha(Request $request)
    {
        [$username] = $request->postMore([
            'username',
        ], true);
        $key = 'login_identifying_code_' . $username;

        return app('json')->success(['is_check' => Cache::get($key) > 2]);
    }

    /**
     * 注释：图形验证码
     * 创建人：Jack
     * 创建时间：2023/3/23 17:15
     * @param Request $request
     * @return mixed
     */
    public function ajcaptcha(Request $request)
    {
        $captchaType = $request->get('captchaType');

        return app('json')->success(aj_captcha_create($captchaType));
    }

    /**
     * 注释：一次验证
     * 创建人：Jack
     * 创建时间：2023/3/23 17:15
     * @param Request $request
     * @return mixed
     */
    public function ajcheck(Request $request)
    {
        [$token, $pointJson, $captchaType] = $request->postMore([
            ['token', ''],
            ['pointJson', ''],
            ['captchaType', ''],
        ], true);
        try {
            aj_captcha_check_one($captchaType, $token, $pointJson);
            return app('json')->success();
        } catch (\Throwable $e) {
            return app('json')->fail(400336);
        }
    }

    /**
     * 注释：login
     * 创建人：Jack
     * 创建时间：2023/3/22 8:18 PM
     * @param Request $request
     * @return mixed
     */
    public function login(Request $request)
    {
        [$username, $password, $captchaType, $captchaVerification] = $request->postMore([
            'username',
            'password',
            ['captchaType', ''],
            ['captchaVerification', ''],
        ], true);
        $key = 'login_identifying_code_' . $username;
        Cache::delete($key);
        if (Cache::has($key) && Cache::get($key) > 2) {
            if (!$captchaType || !$captchaVerification) {
                return app('json')->fail('请拖动滑块验证!');
            }
            // 二次验证
            try {
                aj_captcha_check_two($captchaType, $captchaVerification);
            } catch (\Throwable $e) {
                return app('json')->fail($e->getError());
            }
        }
        validate(\app\validate\admin\setting\SystemAdminValidata::class)->scene('get')->check(['username' => $username, 'password' => $password]);
        $res = $this->services->login($username, $password, 'admin');
        if ($res) {
            Cache::delete($key);
        }

        return app('json')->success($res);
    }

}